package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.AuthorityService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 此类用于权限控制和授予
 */

@RestController
@RequestMapping("/authority")
public class AuthorityController {


    @Reference
    private AuthorityService service;
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page = service.findByPage(queryPageBean);
        return new PageResult(page.getTotal(),page.getRows());

    }


    @RequestMapping("/getMenuByUsername")
    public Result getMenuByUsername( String username){
       LinkedHashSet<Menu> list = service.findMenuByUsername(username);
        List<Map<String,Object>> inList = new ArrayList<>();

        Map<String,Object> maps = new HashMap<>();
        maps.put("path","0");
        maps.put("title","工作台");
        maps.put("icon","fa-dashboard");
        maps.put("children",new ArrayList<>());
        inList.add(maps);
        for (Menu menu : list) {
            if(menu.getParentMenuId()== null){
                String path = menu.getPath();
                String name = menu.getName();
                String icon = menu.getIcon();
                Map<String,Object> map = new HashMap<>();
                map.put("path",path);
                map.put("title",name);
                map.put("icon",icon);
               List<Menu> findMenuById =  service.findMenuById(menu.getId());
                List<Map<String, Object>> list1 = new ArrayList<>();

                if(findMenuById!=null && findMenuById.size()>0){
                    for (Menu menu1 : findMenuById) {
                        Map<String,Object> objectMap = new HashMap<>();
                        String menu1Path = menu1.getPath();
                        String menu1Name = menu1.getName();
                        String linkUrl = menu1.getLinkUrl();
                        objectMap.put("path",menu1Path);
                        objectMap.put("title",menu1Name);
                        objectMap.put("linkUrl",linkUrl);
                        objectMap.put("children",new ArrayList<>());
                        list1.add(objectMap);
                    }
                    map.put("children",list1);
                }

                inList.add(map);


            }
        }
        return new Result(true,"加载列表成功",inList);

    }

    @RequestMapping("/findUserByUsername")
    public Result findUserByUsername(String username){
        try {
            User user = service.findUserByUsername(username);
            return new Result(true,"查询用户信息成功",user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询用户信息失败");
        }

    }


    @RequestMapping("/findRoleCheckedIdByUsername")
    public Result findRoleCheckedIdByUsername(String username){
        try {
            List<Integer> list =  service.findRoleCheckedIdByUsername(username);
            return new Result(true,"查询用户角色id成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询用户角色id失败");
        }
    }


    @RequestMapping("/findRoles")
    public Result findRoles(){
        try {
            List<Role> roles = service.findRoles();
            return new Result(true,"获取角色成功",roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取角色失败");
        }

    }


    @RequestMapping("/edit")
    public Result edit(Integer[] checkitemIds,@RequestBody User user){
        try {
            service.edit(checkitemIds,user);
            return new Result(true,"修改角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改角色失败");
        }
    }


}
